home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / cuj9205.zip / 1005074D < prev    next >
Text File  |  1992-06-02  |  1KB  |  53 lines

  1. /*
  2.  *    Listing 4 - An example of a stack abstraction with magic numbers
  3.  */
  4.  
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <stack.h>
  8.  
  9. static    char    *magic = "Stack";
  10. #define    MAGIC_ON(p)    ((p)->stk_magic = magic)
  11. #define    MAGIC_OFF(p)    ((p)->stk_magic = NULL)
  12. #define    MAGIC_CHECK(p)    assert((p != NULL) && ((p)->stk_magic == magic))
  13.  
  14. /* Create a stack */
  15. STK    *StkConstruct() {
  16. STK    *stk;
  17.  
  18.     stk = malloc(sizeof(STK));
  19.     assert(stk != NULL);
  20.     stk->stk_top = 0;
  21.     MAGIC_ON(stk);
  22.     MAGIC_CHECK(stk);
  23.     return(stk);
  24. }
  25.  
  26. /* Destroy a stack */
  27. void    StkDestroy(STK *stk) {
  28.     MAGIC_CHECK(stk);
  29.     MAGIC_OFF(stk);
  30.     free(stk);
  31. }
  32.  
  33. /* Push an item on the stack */
  34. void    StkPush(STK *stk, void *item) {
  35.     MAGIC_CHECK(stk);
  36.     assert(stk->stk_top < STACK_SIZE);
  37.     stk->stk_stack[stk->stk_top++] = item;
  38. }
  39.  
  40. /* Pop an item from the stack */
  41. void    *StkPop(STK *stk) {
  42.     MAGIC_CHECK(stk);
  43.     assert(stk->stk_top > 0);
  44.     return(stk->stk_stack[--(stk->stk_top)]);
  45. }
  46.  
  47. /* Determine if the stack is empty */
  48. int    StkIsEmpty(STK *stk) {
  49.     MAGIC_CHECK(stk);
  50.     return(stk->stk_top == 0);
  51. }
  52.  
  53.